package com.example.skycodes.servlet;

import com.example.skycodes.bean.NewsBean;
import com.example.skycodes.dao.NewsDao;
import com.sun.deploy.nativesandbox.NativeSandboxBroker;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.example.skycodes.dao.NewsDao.newsDao;

public class NewsUpdateServlet {
    @WebServlet("/newsEdit.jsp")
    public class NewsAddServlet extends HttpServlet {
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            String newsTitle = req.getParameter("upd_NewsTitle");
            String newsContent = req.getParameter("upd_NewsContent");
            int newsId = Integer.parseInt(req.getParameter("newsId")) ;
            NewsBean newsBean = new NewsBean();//实例化新闻对象
            newsBean.setNewsID(newsId);
            newsBean.setNewsTitle(newsTitle);//设置新闻标题
            NativeSandboxBroker.RAF rs = null;
            newsBean.setNewsContent(rs.getString(3));//设置新闻内容
            String user = null;
            newsBean.setAdminName(user);
            newsDao = new NewsDao();//实例化新闻DAO对象
            int result = newsDao.update(newsBean);
            if (result>0){
                resp.sendRedirect("news.jsp");//参数是相对网址
            }else {
                req.setAttribute("msg","更新失败");// 错误页面显示的内容
                req.setAttribute("url","news.jsp");// 显示错误信息后要跳转的页面
                req.getRequestDispatcher("WEB-INF/error.jsp").forward(req,resp);
            }
        }
    }

}
